package com.supor.aiot.common.helper;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.net.Uri;
import android.provider.Settings;

import com.android.baseconfig.common.utils.SystemUtils;

/**
 * ---------------------------------------------------------------- <br>
 * Copyright (C) 2014-2020, by het, Shenzhen, All rights reserved.  <br>
 * ---------------------------------------------------------------- <br>
 * <p>
 * 描述: ActivityHelper <br>
 * 作者: lei <br>
 * 日期: 2020/12/4 <br>
 */
public class ActivityHelper {

    public static void gotoWiFiSetting(Activity activity) {
        gotoWiFiSetting(activity, -1);
    }

    public static void gotoWiFiSetting(Activity activity, int requestCode) {
        // Logc.d("brand: " + SystemUtils.getBrand() + ", model: " + SystemUtils.getSystemModel());
        // if ("HUAWEI".equalsIgnoreCase(SystemUtils.getBrand())) {
        try {
            Intent intent = new Intent(activity.getPackageName());
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            ComponentName comp = new ComponentName("com.android.settings",
                    "com.android.settings.Settings$WifiSettingsActivity");
            intent.setComponent(comp);
            if (requestCode == -1) {
                activity.startActivity(intent);
            } else {
                activity.startActivityForResult(intent, requestCode);
            }
        } catch (Exception e) {
            if (requestCode == -1) {
                activity.startActivity(new Intent(Settings.ACTION_WIRELESS_SETTINGS));
            } else {
                activity.startActivityForResult(new Intent(Settings.ACTION_WIRELESS_SETTINGS), requestCode);
            }
            e.printStackTrace();
        }
    }

    /**
     * 跳转系统设置定位
     */
    public static void gotoGpsSetting(Activity activity, int requestCode) {
        // 转到手机设置界面，用户设置GPS
        Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
        activity.startActivityForResult(intent, requestCode); // 设置完成后返回到原来的界面
    }

    public static void gotoApSetting(Activity activity, int requestCode) {
        try {
            String brand = SystemUtils.getBrand();
            Intent intent = new Intent();
            intent.addCategory(Intent.CATEGORY_DEFAULT);
            intent.setAction("android.intent.action.MAIN");
            ComponentName cn;
            // 华为手机暂时找不到跳转个人热点页面方法，只能跳转到移动网络页面
            if ("HUAWEI".equalsIgnoreCase(brand) || "HONOR".equalsIgnoreCase(brand)) {
                cn = new ComponentName("com.android.settings",
                        "com.android.settings.Settings$WirelessSettingsActivity");
            } else {
                cn = new ComponentName("com.android.settings",
                        "com.android.settings.Settings$TetherSettingsActivity");
            }
            intent.setComponent(cn);
            activity.startActivityForResult(intent, requestCode);
        } catch (Exception e) {
            Intent intent =  new Intent(Settings.ACTION_SETTINGS);
            activity.startActivity(intent);
            e.printStackTrace();
        }

    }

    // 启动应用的设置
    public static void startAppSettings(Activity activity, int requestCode) {
        Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
        intent.setData(Uri.parse("package:" + activity.getPackageName()));
        activity.startActivityForResult(intent, requestCode);
    }
}
